package servlet;

import com.google.zxing.WriterException;
import qrcode.QRCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;

/**
 * Created by Administrator on 2016/4/27.
 */
@WebServlet(name = "MakeQRCodeServlet")
public class MakeQRCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Writer out = response.getWriter();
        File file = new File("../webapps/fmdksxf/src/MP3/");
//        out.write(file.getAbsolutePath()+'\n');
//        out.write(file.getCanonicalPath()+'\n');
        File[] files = file.listFiles();
//        out.write(getFileNameNoEx(files[0].getName())+'\n');
        for (File f :
                files) {
            QRCode qrCode = new QRCode();
            try {
                qrCode.makeQRCode("http://www.eight-fifteen.click/fmdksxf/?name="+f.getName(),getFileNameNoEx(f.getName()));
            } catch (WriterException e) {
                e.printStackTrace();
            }
        }
    }
    public  String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot >-1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }
}
